package com.qf.web;

import com.github.pagehelper.PageInfo;
import com.qf.bean.Training;
import com.qf.service.FuHeService;
import com.qf.utils.filter.enuml.FuHeEnum;
import com.qf.utils.filter.enuml.PageEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

/**
 * IntelliJ IDEA -> person
 * 2019/11/23 8:53
 *
 * @author zmyao
 * 别看，我是渣渣。
 */
@Controller
public class FuHeController {
    @Autowired
    private FuHeService fuHeService;
@RequestMapping("/peixun/peixunjihua/updateAuditopinionToFuHe")
    public void  updateAuditopinionToFuHe(int id, HttpServletResponse response){
    try {
        int len = fuHeService.updateByIdSetAuditopinion(id, FuHeEnum.CHECK.getAuditopinion());
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        if (len > 0){
         out.print("<script>alert('复核通过');location.href='/peixun/peixunjihua/findall';</script>");
        }
        else {
            out.print("<script>alert('复核不通过');location.href='/peixun/peixunjihua/findTrainingByIdUpdate?id="+id+"';</script>");
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
@RequestMapping("/peixun/peixunfuhe/findAllFuHe")
    public String findAllFuHe(@RequestParam(defaultValue = "1") int  index, String trainingname, String  trainingdateStart, String  trainingdateEnd, ModelMap map){
    PageInfo<Training> pageInfo = fuHeService.findAllByOpintion(index, PageEnum.PAGESIZE.getSize(), trainingname, trainingdateStart, trainingdateEnd, FuHeEnum.CHECK.getAuditopinion());
    map.addAttribute("pageInfo",pageInfo);
    map.addAttribute("trainingname",trainingname);
    map.addAttribute("trainingdateStart",trainingdateStart);
    map.addAttribute("trainingdateEnd",trainingdateEnd);
    return  "/peixun/peixunfuhe/list";
}

@RequestMapping("/peixun/peixunfuhe/findFuHeById")
    public String findFuHeById(Integer id,ModelMap map){
    Training training = fuHeService.findTrainingByIdOpinion(id, FuHeEnum.CHECK.getAuditopinion());
    map.addAttribute("training",training);
    return  "/peixun/peixunfuhe/fuhe";
}

@RequestMapping("/peixun/peixunfuhe/updateOpintion")
    public void updateOpintionToPass(Training training,HttpServletResponse response,String method){
    try {
        System.out.println("method"+method);
        System.out.println("result"+training);
            response.setContentType("text/html;charset=utf-8");
            PrintWriter   out = response.getWriter();
        if ("pass".equals(method)){
            int len = fuHeService.updateOpintion(training, FuHeEnum.PASS.getAuditopinion());
            if (len > 0 ){
                out.print("<script>alert('审核通过');location.href='/peixun/peixunfuhe/findAllFuHe';</script>");
            }
            else {
                out.print("<script>alert('审核不通过');location.href='/peixun/peixunfuhe/findFuHeById?id="+training.getId()+"';</script>");
            }

        }
        else  if ("new".equals(method)){
            int len = fuHeService.updateOpintion(training, FuHeEnum.NEW.getAuditopinion());
            if (len > 0){
                out.print("<script>alert('审核驳回');location.href='/peixun/peixunfuhe/findAllFuHe';</script>");
            }  else {
                out.print("<script>alert('审核驳回失败');location.href='/peixun/peixunfuhe/findFuHeById?id="+training.getId()+"';</script>");
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

}

}
